package com.huluobo.a2202mvvm_im_douyindemo.chat;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseRepo;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMMessage;

/**
 * Created by LC on 2024/4/29.
 */
public class ChatRepo extends BaseRepo {
    public void sendMessage(String username, String content, int chatType, MutableLiveData<EMMessage> success,
                            MutableLiveData<String> failed) {
        //构建一条消息
        EMMessage emMessage = EMMessage.createTextSendMessage(content, username);
        //设置消息类型
        switch (chatType) {
            case 1:
                emMessage.setChatType(EMMessage.ChatType.Chat);
                break;
            case 2:
                emMessage.setChatType(EMMessage.ChatType.GroupChat);
                break;
            case 3:
                emMessage.setChatType(EMMessage.ChatType.ChatRoom);
                break;
        }

        emMessage.setMessageStatusCallback(new EMCallBack() {
            @Override
            public void onSuccess() {
                success.postValue(emMessage);
            }

            @Override
            public void onError(int code, String error) {
                failed.postValue(error);
            }
        });

        //通过环信sdk把消息发送出去
        emClient.chatManager().sendMessage(emMessage);
    }
}
